package com.roger.myHome.controller;

import com.roger.myHome.service.CacheService;
import com.roger.myHome.util.*;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LogoutController {
    @Resource
    private CacheService cacheService;

    @Resource
    private UserHelper userHelper;

    @PostMapping("/logout")
    public String logout(HttpServletRequest request){
        log.info("[{}],[{}]登出系统", userHelper.getLoginUser(request), DateUtil.getCurrentTime());
        String header = request.getHeader("header");
        Claims claims = JwtUtil.checkToken(header);
        String id = (String)claims.get(Constants.USER_ID);
        cacheService.removeCache(Long.valueOf(id));
        Map hashMap = new HashMap();

        return StringUtils.getJSONString(hashMap);
    }
}
